/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

/* 
 * Hum -- The Low Calorie Music Manager
 * 
 * Copyright (C) 2007 by Brian Davis <brian.william.davis@gmail.com>
 *
 * This file is modifiable/redistributable under the terms of the GNU
 * General Public License.
 *
 * You should have recieved a copy of the GNU General Public License along
 * with this program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 0211-1307, USA.
 */

#include "hum_utils.h"

// FIXME: This should do real logging via g_log and friends.
void
hum_debug (const gchar* func, gchar *fmt, ...)
{
	va_list va;
	gchar *msg;
	
	if DEBUG
	{
		va_start (va, fmt);
		msg = g_strjoin (NULL, func, ": ", fmt, NULL);
		g_vprintf (msg, va);
		va_end (va);
	}
}

gchar *
hum_str_from_time (guint64 time)//, HumFormat format)
{
	gchar *time_str;
	
	if (time < 60) // less than a minute
		time_str = g_strdup_printf ("%u seconds", (guint) time);
	else if (time < 3600) // less than an hour
		time_str = g_strdup_printf ("%u minutes", (guint) (time / 60));
	else if (time < 86400) // less than a day
		time_str = g_strdup_printf ("%u hours", (guint) (time / 3600));
	else if (time < 604800) // less than a week
		time_str = g_strdup_printf ("%u days", (guint) (time / 86400));
	else if (time < 18144000) // less than a month
		time_str = g_strdup_printf ("%u weeks", (guint) (time / 604800));
	else if (time < 217728000) // less than a year
		time_str = g_strdup_printf ("%u months", (guint) (time / 18144000));
	else
		time_str = g_strdup_printf ("%u years", (guint) (time / 217728000));
	
	return time_str;
}

gchar *
hum_str_from_size (guint64 size)
{
	gchar *size_str;
	
	if (hum_app.total_song_size < 1000) // less than a kilobyte
		size_str = g_strdup_printf ("%u B", (guint) hum_app.total_song_size);
	else if (hum_app.total_song_size < 1000000) // less than a megabyte
		size_str = g_strdup_printf ("%.1f KB", (gdouble) hum_app.total_song_size / (gdouble) 1024);
	else if (hum_app.total_song_size < 1000000000) // less than a gigabyte
		size_str = g_strdup_printf ("%.1f MB", (gdouble) hum_app.total_song_size / (gdouble) 1000000);
	else if (hum_app.total_song_size < G_GINT64_CONSTANT(1000000000000)) // less than a terabyte
		size_str = g_strdup_printf ("%.1f GB", (gdouble) hum_app.total_song_size / (gdouble) 1000000000);
	//else if (hum_app.total_song_size < 1000000000000000) // less than a petabyte
	//	size_str = g_strdup_printf ("%.1f TB", (gdouble) hum_app.total_song_size / (gdouble) 1000000000000);
	//else if (hum_app.total_song_size < 1000000000000000000) // less than a exabyte
	//	size_str = g_strdup_printf ("%.1f PB", (gdouble) hum_app.total_song_size / (gdouble) 1000000000000000);
	else
		size_str = g_strdup_printf ("%.1f TB", (gdouble) hum_app.total_song_size / (gdouble) G_GINT64_CONSTANT(10000000000000));
		
	return size_str;
}

gint
hum_utf8_strcmp (gchar *a, gchar *b)
{
	gchar *norm_a, *norm_b, *key_a, *key_b;
	guint sort;
	
	//g_print ("hum_utf8_srcmp: triggered...\n");
	
	/* Normalize case. */
	norm_a = g_utf8_casefold (a, -1);
	norm_b = g_utf8_casefold (b, -1);
	
	/* Speed up sort by generating a key (to avoid comparing the whole string). */
	key_a = g_utf8_collate_key (norm_a, -1);
	key_b = g_utf8_collate_key (norm_b, -1);
	
	sort = strcmp (key_a, key_b);
	
	g_free (norm_a);
	g_free (norm_b);
	g_free (key_a);
	g_free (key_b);
	
	return sort;
}

gchar *
hum_remove_escapes (gchar* str)
{
	gchar *delimiter = "\'\\\'";
	gchar *next = NULL;
	
	next = strstr (str, delimiter);
	
	while (next)
	{
		//g_print ("hum_remove_escapes: found %s at %u, incrementing past it to %u, which is %s...\n", next, (guint) &next, (guint) (&next + 3), next + 3);
		strcpy (next, next + strlen (delimiter));
		next = strstr (str, delimiter);
	}
	
	g_free (delimiter);
	g_free (next);
	
	//g_print ("hum_remove_escapes: %s stripped\n", str);
	return str;
}

